const { Sequelize, Model, INTEGER } = require('sequelize')
const bcryptjs = require('bcryptjs')
const { db } = require('../core/sequelize')
const { NotFound, AuthFailed } = require('../core/http-exception')
class User extends Model {
  //static关键字静态方法 不用new 可以直接调用
  static async verifyEmailPass(email, pass) {
    const user = await User.findOne({
      where: {
        email,
      },
    })
    if (!user) {
      throw new NotFound('账号未找到')
    }
    const correct = bcryptjs.compareSync(pass, user.password)
    if (!correct) {
      throw new AuthFailed('密码不正确')
    }
    return user
  }
  static async verifyPhonePass(phone, pass) {

    const user = await User.findOne({
      where: {
        phone,
      },
    })
    if (!user) {
      throw new NotFound('账号未找到')
    }
    const correct = bcryptjs.compareSync(pass, user.password)
    if (!correct) {
      throw new AuthFailed('密码不正确')
    }
    return user
  }
  // 修改可爱币
  static async setIntegral(userId, integralNumber, isAdd) {
    const userInfo = await User.getUserByUserUserId(userId)
    let { integral } = userInfo
    integral = integral ? integral : 0
    if (isAdd && integralNumber > integral) {
      return false
    }
    const num = integralNumber ? integralNumber : 100
    const setInt = await User.update(
      {
        integral: isAdd ? integral - num : integral + num //要修改的数据
      },
      {
        where: { id: userId } //查询修改项的条件
      }
    )
    return setInt
  }
  //查询是否存在ID
  static async getUserByUserUserId(userId) {
    const user = await User.findOne({
      where: {
        id: userId
      },
    })
    return user
  }
  //查询是否存在openid
  static async getUserByUserOpenId(openid) {
    const user = await User.findOne({
      where: {
        openid,
      },
    })
    return user
  }
  //添加openid
  static async registerByUserOpenId(openid, userInfo) {
    return await User.create({
      openid,
      integral: 0,
      ...userInfo
    })
  }
}
//创建用户
User.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true, //设置主键
      autoIncrement: true,
    },
    openid: {
      type: Sequelize.STRING(64), //最大64位
      unique: true, //唯一性
    },
    integral: Sequelize.INTEGER,
    vip: Sequelize.INTEGER,
    nickName: Sequelize.STRING, //类型 字符串
    avatarUrl: Sequelize.STRING, //类型 字符串
    city: Sequelize.STRING, //类型 字符串
    gender: Sequelize.STRING, //类型 字符串
    type: Sequelize.STRING, //类型 字符串
    occupation: Sequelize.STRING, //类型 字符串
    password: {
      type: Sequelize.STRING,
      set(val) {
        const salt = bcryptjs.genSaltSync(10)
        const paw = bcryptjs.hashSync(val, salt)
        this.setDataValue('password', paw)
      },
    },

    email: {
      type: Sequelize.STRING(64), //最大64位
      unique: true, //唯一性
    },
    phone: {
      type: Sequelize.STRING(11), //最大64位
      unique: true, //唯一性
    },
  },
  {
    sequelize: db,
    modelName: 'users',
    timestamps: false
  }
)
module.exports = { User }
